<template>
  <Form :model="data" :rules="rules" mode="twocolumn" ref="form" :labelWidth="120" style="width: 800px">
    <FormItem label="名称" prop="name" required>
      <Input type="text" v-model="data.name"/>
    </FormItem>
    <FormItem label="使用模型" prop="mode" required>
      <Select :datas="models" v-model="data.mode" @change="onModelChange"/>
    </FormItem>
    <FormItem label="系统提示词" prop="prompt" single>
      <Textarea type="text" v-model="data.prompt" placeholder="模型人设，主要用于人设设定，例如，你是xxx公司制作的AI助手"/>
    </FormItem>
    <FormItem label="说明" prop="memo" single>
      <Textarea type="text" v-model="data.memo" placeholder="应用的说明，没有什么特殊用途"/>
    </FormItem>

    <FormItem label="AI HOST" prop="aiHost" single required>
      <Input type="text" v-model="data.aiHost"/>
    </FormItem>
    <FormItem label="AI KEY" prop="aiKey" single required>
      <Input type="text" v-model="data.aiKey" placeholder="Secret Key,文心一言格式为:APIKEY-SecretKey"/>
    </FormItem>

    <FormItem label="热度" prop="temperature">
      <div class="h-input-group">
        <NumberInput v-model="data.temperature" max="1" min="0.1" :precision="1" step="0.1" useOperate/>
        <span class="h-input-addon">
           <Tooltip>
             <template #content>
               <div>说明：</div>
               <div> （1）较高的数值会使输出更加随机，而较低的数值会使其更加集中和确定</div>
               <div> （2）默认0.8，范围 (0, 1.0]，不能为0</div>
             </template>
          <i class="h-icon-info" style="margin-left: 3px"></i>
        </Tooltip>
        </span>
      </div>
    </FormItem>
    <FormItem label="多样性" prop="topP">
      <div class="h-input-group">
        <NumberInput v-model="data.topP" max="1" min="0.1" :precision="1" step="0.1" useOperate/>
        <span class="h-input-addon">
           <Tooltip>
             <template #content>
               <div>说明：</div>
               <div> （1）影响输出文本的多样性，取值越大，生成文本的多样性越强</div>
               <div> （2）默认0.8，取值范围 [0, 1.0]</div>
             </template>
            <i class="h-icon-info" style="margin-left: 3px"></i>
          </Tooltip>
        </span>
      </div>
    </FormItem>
    <FormItem label="惩罚" prop="penaltyScore">
      <div class="h-input-group">
        <NumberInput v-model="data.penaltyScore" max="2.0" min="1.0" :precision="1" step="0.1" useOperate/>
        <span class="h-input-addon">
           <Tooltip>
             <template #content>
               <div>通过对已生成的token增加惩罚，减少重复生成的现象。说明：</div>
               <div> （1）值越大表示惩罚越大</div>
               <div> （2）默认1.0，取值范围：[1.0, 2.0]</div>
             </template>
            <i class="h-icon-info" style="margin-left: 3px"></i>
          </Tooltip>
        </span>
      </div>

    </FormItem>
    <FormItem label="禁用搜索" prop="disableSearch">
      <HSwitch v-model="data.disableSearch"></HSwitch>
    </FormItem>
    <FormItem label="应用密钥" prop="appKey">
      <Input type="text" v-model="data.appKey"/>
    </FormItem>
    <FormItem label="是否启用" prop="enable">
      <HSwitch v-model="data.enable"></HSwitch>
    </FormItem>
  </Form>
</template>
<script setup>
import {ajax} from 'framework'
import {ref, reactive} from 'vue'
import {message} from 'heyui'

const models=[
  {key:'gpt-3.5-turbo',title:'GPT 3.5',host:'https://api.openai.com/'},
  {key:'gpt-4',title:'GPT 4',host: 'https://api.openai.com/'},
  {key:'ERNIE-Bot',title:'文心一言',host: 'https://aip.baidubce.com/'},
  {key:'ERNIE-Bot 4.0',title:'文心一言4.0',host: 'https://aip.baidubce.com/'}
]
//事件
//Props Open方法传入的参数
const {params, dialog} = defineProps({
  params: Object,
  dialog: Object
})

//dialog.cancel() 方法可以直接关闭对话框
//打开Dialog时进行初始化
let isAdd = false
if (params) {
  //通知Dialog处于loading状态
  dialog.loading(true)
  ajax.get("application/detail", params).then(r => {
    Object.assign(data, r.data)
    dialog.loading(false)
  })
} else {
  isAdd = true
}

//属性和状态
let form = ref(null)

//表单数据
const data = reactive({
  name: null,
  prompt: null,
  memo: null,
  iconImg: null,
  aiHost: null,
  aiKey: null,
  mode: null,
  temperature: null,
  topP: null,
  penaltyScore: null,
  disableSearch: null,
  appKey: null,
  enable: null,
})

//验证规则
const rules = {}

/**
 * 提交数据
 */
dialog.onSubmit(() => {
  let validResult = form.value.valid();
  if (validResult.result) {
    dialog.loading(true)
    ajax.post(isAdd ? 'application/add' : 'application/save', data).then(r => {
      message("已保存")
      dialog.success(r.data)
    }).catch(e => {
      message.error(e.message)
    }).finally(f => dialog.loading(false))
  }
})
function onModelChange(v){
  if(!data.aiHost){
    data.aiHost=v.host
  }
}
</script>
